<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Area{
    public function __construct(){
        $this->CI= &get_instance();
    }

    // 获取全部学校区域数据，格式：省市区
    public function get_school_areas_to_area() {
        $schoolList = $this->CI->schoolModel->get_school_list();
        foreach($schoolList as $school) {
            // 获取所有学校的一级地区
            $provinces[] = array('code' => $school['school_province']);
            // 获取所有学校的二级地区
            $citys[] = array('code' => $school['school_city']);
            // 获取所有学校的三级地区
            $areas[] = array('code' => $school['school_area']);
        }
        // 去重
        $provinces = array_values(array_unique($provinces, SORT_REGULAR));
        $citys = array_values(array_unique($citys, SORT_REGULAR));
        $areas = array_values(array_unique($areas, SORT_REGULAR));
        // 二级地区添加三级地区
        foreach($citys as &$city) {
            $cityInfo = $this->CI->schoolModel->get_area_info($city['code']);
            $city['name'] = $cityInfo['area_name'];
            foreach($areas as $area) {
                $areaInfo = $this->CI->schoolModel->get_area_info($area['code']);
                if($areaInfo['parent_id'] === $cityInfo['id']) {
                    $city['child'][] = array(
                        'code' => $area['code'],
                        'name' => $areaInfo['area_name']
                    );
                }
            }
        }
        // 一级地区添加二级地区
        foreach($provinces as &$province) {
            $provinceInfo = $this->CI->schoolModel->get_area_info($province['code']);
            $province['name'] = $provinceInfo['area_name'];          
            foreach($citys as $city2) {
                $cityInfo = $this->CI->schoolModel->get_area_info($city2['code']);
                if($cityInfo['parent_id'] === $provinceInfo['id']) {
                    $province['child'][] = $city2;
                }
            }
        }
        return $provinces;
    }

    // 获取全部学校区域数据，格式：省市
    public function get_school_areas_to_city() {
        $schoolList = $this->CI->schoolModel->get_school_list();
        foreach($schoolList as $school) {
            // 获取所有学校的一级地区
            $provinces[] = array('code' => $school['school_province']);
            // 获取所有学校的二级地区
            $citys[] = array('code' => $school['school_city']);
        }
        // 去重
        $provinces = array_values(array_unique($provinces, SORT_REGULAR));
        $citys = array_values(array_unique($citys, SORT_REGULAR));
        // 一级地区添加二级地区
        foreach($provinces as &$province) {
            $provinceInfo = $this->CI->schoolModel->get_area_info($province['code']);
            $province['name'] = $provinceInfo['area_name'];          
            foreach($citys as $city2) {
                $cityInfo = $this->CI->schoolModel->get_area_info($city2['code']);
                if($cityInfo['parent_id'] === $provinceInfo['id']) {
                    $city2['name'] = $cityInfo['area_name'];
                    $province['child'][] = $city2;
                }
            }
        }
        return $provinces;
    }
    
    // 获取所有的地区，按上下级排序
    public function get_all_areas() {
        // 获取一级地区
        $params['field'] = 'id, area_name AS name';
        $params['parentId'] = -1;
        $provinces = $this->CI->schoolModel->get_area_list($params);
        // 获取二级地区
        foreach($provinces as &$province) {
            $params['parentId'] = $province['id'];
            $citys = $this->CI->schoolModel->get_area_list($params);
            // 获取三级地区
            foreach($citys as &$city) {
                $params['parentId'] = $city['id'];
                $districts = $this->CI->schoolModel->get_area_list($params);
                $city['child'] = $districts;
            }
            $province['child'] = $citys;
        }
        return $provinces;
    }
}